		TITLE	PNEWSEG - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	SEGMSYMS
if	fg_pe
		INCLUDE	PE_STRUC
endif

		PUBLIC	PROT_OUT_NEW_SEGMENT,PROT_OUT_NEW_SEGMOD


		.DATA

		EXTERNDEF	FIX2_SEG_COMBINE:BYTE,FIX2_SEG_TYPE:BYTE

		EXTERNDEF	OPTI_STOSD_SIZE:DWORD,OPTI_RELOC_CLEAR:DWORD,LAST_SEG_OS2_NUMBER:DWORD,FIX2_SEG_OS2_NUMBER:DWORD
		EXTERNDEF	FIX2_LDATA_SEGMENT_GINDEX:DWORD,SEGMENT_TABLE:DWORD,SEGMENT_TABLE_PTR:DWORD,FIX2_SEG_OFFSET:DWORD
		EXTERNDEF	FIX2_EXEPACK_BASE:DWORD,FIX2_SEG_LEN:DWORD

		EXTERNDEF	PE_OBJECT_GARRAY:STD_PTR_S,SEGMENT_GARRAY:STD_PTR_S

		EXTERNDEF	OUT_FLUSH_SEGMENT:DWORD,OUT_FLUSH_EXE:DWORD


		.CODE	PASS2_TEXT

		EXTERNDEF	NEW_SEGMOD_RELOC:PROC,EONSM_DEBUG:PROC,NEW_SEGMOD_REAL:PROC,DEFINE_STACK_DELTA:PROC
		EXTERNDEF	REAL_STACK_SEGMOD:PROC


PROT_OUT_NEW_SEGMENT	PROC
		;
		;WE NEED TO DETERMINE THINGS LIKE IS THIS NOW A DEBUG
		;RECORD?  IS THIS A BUNCH OF 'COMMON' SEGMODS?
		;
		MOV	AL,FIX2_SEG_TYPE
		MOV	ECX,LAST_SEG_OS2_NUMBER

		TEST	AL,MASK SEG_CV_TYPES1 + MASK SEG_CV_SYMBOLS1	;IGNORE IF CODEVIEW
		JNZ	L1$
		;
		;IF THIS SEGMENT # IS DIFFERENT THAN THE LAST ONE, FLUSH
		;SEGMENT AND RELOC INFO...
		;
		MOV	EAX,FIX2_SEG_OS2_NUMBER
		MOV	DL,FIX2_SEG_COMBINE

		CMP	EAX,ECX
		JNZ	L2$
L29$:
		CMP	DL,SC_STACK
		JZ	DEFINE_STACK_DELTA

		MOV	EAX,FIX2_SEG_LEN	;DO IN CASE OF COMMON
		JMP	NEW_SEGMOD_RELOC

L1$:
		RET

L2$:
		CALL	OUT_FLUSH_SEGMENT

		MOV	EDX,0FFH
		MOV	EAX,FIX2_SEG_OS2_NUMBER

		RESS	OS2_PAGE_ALIGN_DONE,DH
		MOV	LAST_SEG_OS2_NUMBER,EAX
		;
		;IF CODE SEGMENT, DISABLE EXEPACK
		;
		CONV_EAX_SEGTBL_ECX

		MOV	EAX,[ECX]._SEGTBL_FLAGS
		RESS	EXEPACK_BODY,DH

		AND	AL,1		;DATA = 1
		JZ	L4$

		BITT	EXEPACK_SELECTED,DH
		JZ	L4$

		SETT	EXEPACK_BODY,DL
L4$:

		MOV	EAX,[ECX]._SEGTBL_LSIZE	;NEED TO SET OPTI_STOSD

		MOV	EDX,EAX
		ADD	EAX,31

		CMP	EDX,PAGE_SIZE
		JBE	L5$

		MOV	EDX,PAGE_SIZE

L5$:
		SHR	EAX,4			;# OF DWORDS TO CLEAR FOR RELOC
		ADD	EDX,3

		SHR	EDX,2
		MOV	OPTI_RELOC_CLEAR,EAX

		MOV	OPTI_STOSD_SIZE,EDX
		XOR	EAX,EAX

		MOV	[ECX]._SEGTBL_FADDR,EAX
		MOV	[ECX]._SEGTBL_PSIZE,EAX

		MOV	ECX,FIX2_SEG_OFFSET
		MOV	DL,FIX2_SEG_COMBINE

		MOV	FIX2_EXEPACK_BASE,ECX
		JMP	L29$

PROT_OUT_NEW_SEGMENT	ENDP


if	fg_pe
		PUBLIC	PE_OUT_NEW_SEGMENT


PE_OUT_NEW_SEGMENT	PROC
		;
		;WE NEED TO DETERMINE THINGS LIKE IS THIS NOW A DEBUG
		;RECORD?  IS THIS A BUNCH OF 'COMMON' SEGMODS?
		;
		MOV	AL,FIX2_SEG_TYPE
		MOV	ECX,FIX2_SEG_OS2_NUMBER

		TEST	AL,MASK SEG_CV_TYPES1 + MASK SEG_CV_SYMBOLS1	;IGNORE IF CODEVIEW
		JNZ	L1$
		;
		;IF THIS SEGMENT # IS DIFFERENT THAN THE LAST ONE, FLUSH
		;SEGMENT AND RELOC INFO...
		;
		MOV	EAX,LAST_SEG_OS2_NUMBER
		MOV	DL,FIX2_SEG_COMBINE

		CMP	ECX,EAX
		JNZ	L2$
L29$:
		CMP	DL,SC_STACK
		JZ	DEFINE_STACK_DELTA

		MOV	EAX,FIX2_SEG_LEN	;DO IN CASE OF COMMON
		JMP	NEW_SEGMOD_RELOC

L1$:
		RET

L2$:
		CALL	OUT_FLUSH_SEGMENT	;CLOSE LAST PE_SECTION ?

		PUSHM	EDI,ESI

		MOV	EDI,FIX2_SEG_OS2_NUMBER
		MOV	ESI,FIX2_LDATA_SEGMENT_GINDEX

		MOV	LAST_SEG_OS2_NUMBER,EDI

		CONVERT	EDI,EDI,PE_OBJECT_GARRAY
		ASSUME	EDI:PTR PE_OBJECT_STRUCT

		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		ADD	ESI,SEGMENT_STRUCT._SEG_TEXT

		MOV	EDX,[EDI]._PEOBJECT_VSIZE
		LEA	EDI,[EDI]._PEOBJECT_NAME

		MOV	ECX,8
L21$:
		MOV	AL,BYTE PTR[ESI]
		INC	ESI

		MOV	BYTE PTR[EDI],AL
		INC	EDI

		OR	AL,AL
		JZ	L22$

		DEC	ECX
		JNZ	L21$
L22$:
		MOV	ECX,PAGE_SIZE

		CMP	EDX,ECX
		JNC	L5$

		LEA	ECX,[EDX+3]
L5$:
		SHR	ECX,2
		MOV	EAX,FIX2_SEG_OFFSET

		MOV	OPTI_STOSD_SIZE,ECX
		MOV	FIX2_EXEPACK_BASE,EAX

		POPM	ESI,EDI

		MOV	DL,FIX2_SEG_COMBINE
		JMP	L29$

PE_OUT_NEW_SEGMENT	ENDP

endif


PROT_OUT_NEW_SEGMOD	PROC
		;
		;I'M NOT SURE WHAT SIGNIFICANCE THIS HAS, EXCEPT DURING
		;CODEVIEW INFO FOR STARTING AN INDEX ENTRY...
		;
		MOV	AL,FIX2_SEG_COMBINE
		GETT	CL,DOING_DEBUG

		CMP	AL,SC_COMMON
		JZ	L1$

		CMP	AL,SC_STACK
		JZ	REAL_STACK_SEGMOD

		OR	CL,CL
		JZ	NEW_SEGMOD_REAL		;USE SM_LEN-SM_START TO DEFINE OPTI_RELOC_CLEAR AND OPTI_STOSD_SIZE, DO RELOC_CLEAR

		CALL	NEW_SEGMOD_REAL

		JMP	EONSM_DEBUG

L1$:
		RET

PROT_OUT_NEW_SEGMOD	ENDP


		END

